package com.fr.report.js;

import com.fr.base.FRContext;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.report.cellElement.Formula;
import com.fr.report.parameter.Parameter;
import com.fr.web.Repository;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ParameterJavaScript extends AbstractJavaScript
{
  public String createJS(Repository paramRepository)
  {
    Map localMap = paramRepository.checkoutTplContext();
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("$.ajax({");
    localStringBuffer.append("url:'${servletURL}?op=dialog_parameters&sessionID=${sessionID}',");
    localStringBuffer.append("type:'POST',");
    localStringBuffer.append("data:");
    Parameter[] arrayOfParameter = getParameters();
    JSONObject localJSONObject = new JSONObject();
    for (int i = 0; i < arrayOfParameter.length; ++i)
      try
      {
        if (!(arrayOfParameter[i].getValue() instanceof Formula))
          localJSONObject.put(arrayOfParameter[i].getName(), arrayOfParameter[i].getValue());
        else
          localJSONObject.put(arrayOfParameter[i].getName(), ((Formula)arrayOfParameter[i].getValue()).getResult());
      }
      catch (JSONException localJSONException)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      }
    localStringBuffer.append(localJSONObject.toString());
    localStringBuffer.append(",");
    localStringBuffer.append("complete : function(){FR.SessionMgr.get('${sessionID}').loadContentPane()}");
    localStringBuffer.append("});");
    String str = localStringBuffer.toString();
    try
    {
      return TemplateUtils.render(str, localMap);
    }
    catch (Exception localException)
    {
    }
    return str;
  }
}